package com.jieshuibao.service;

import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.GainmedalMapper;
import com.jieshuibao.mapper.RedEnvelopesMapper;
import com.jieshuibao.mapper.UserDealRecordMapper;
import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.model.db.Gainmedal;
import com.jieshuibao.model.db.GainmedalExample;
import com.jieshuibao.model.db.RedEnvelopes;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.UserDealRecord;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class GainmedalService{
	
	@Resource
	private GainmedalMapper gainmedalMapper;
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private RedEnvelopesMapper redEnvelopesMapper;
	
	@Resource
	private UserDealRecordMapper userDealRecordMapper;
	
	@Resource
	private RedisService redisService;
	
	public void addGainmedal(Gainmedal gainmedal) {
		gainmedalMapper.insertSelective(gainmedal);
	}
	
	public void delGainmedalByPhone(String phone) {
		gainmedalMapper.deleteByPrimaryKey(phone);
	}
	
	public void updateGainmedal(Gainmedal gainmedal) {
		gainmedalMapper.updateByPrimaryKeySelective(gainmedal);
	}
	
	public Gainmedal getGainmedalByPhone(String phone) {
		return gainmedalMapper.selectByPrimaryKey(phone);
	}
	
	public List<Gainmedal> getPageGainmedal(GainmedalExample gainmedalExample) {
		return gainmedalMapper.selectPage(gainmedalExample);
	}
	public List<Gainmedal> getGainmedals(GainmedalExample gainmedalExample) {
		return gainmedalMapper.selectByExample(gainmedalExample);
	}
	public int countGainmedal(GainmedalExample gainmedalExample) {
		return gainmedalMapper.countByExample(gainmedalExample);
	}
	
	//随机获得勋章
	public int updateAndAddGainmedal(Gainmedal gainmedal,double money,User user){
		int c=0;
		try {
			Gainmedal gainmedal2 = new Gainmedal();
			String phoneNumber = user.getPhoneNumber();
			double balance = user.getBalance();
			gainmedal2.setPhone(phoneNumber);
			
			User user2 = new User();
			user2.setPhoneNumber(phoneNumber);
			user2.setBalance(balance+money);
			Date date = new Date();
			
			//红包
			RedEnvelopes redEnvelopes = new RedEnvelopes();
			String uuid = UUIDGenerator.getUUID();
			redEnvelopes.setMoney(money);
			redEnvelopes.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			redEnvelopes.setReceiveDate(date);
			redEnvelopes.setRedStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			redEnvelopes.setRedType(PayTypeUtil.REDENVELPES_NEWYEAR);
			redEnvelopes.setSendDate(date);
			redEnvelopes.setSendPhoneNum(PayTypeUtil.COMPANY_ACCPUNT);
			redEnvelopes.setUserPhoneNum(phoneNumber);
			redEnvelopes.setUuid(uuid);
			
			
			//流水
			UserDealRecord userDealRecord1 = new UserDealRecord();
			UserDealRecord userDealRecord2 = new UserDealRecord();
			 String uuid1 = UUIDGenerator.getUUID();//用户流水
			 String uuid2 = UUIDGenerator.getUUID();//公司流水
			 userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
			 userDealRecord1.setBusinessNo(uuid);
			 userDealRecord1.setCreateDate(date);
			 userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_ENVELOPES);
			 userDealRecord1.setPaySucTime(date);
			 userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			 userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			 userDealRecord1.setSum(money);
			 userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
			 userDealRecord1.setUserId(phoneNumber);
			 userDealRecord1.setUuid(uuid1);
			 
			 userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
			 userDealRecord2.setBusinessNo(uuid);
			 userDealRecord2.setCreateDate(date);
			 userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_ENVELOPES);
			 userDealRecord2.setPaySucTime(date);
			 userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			 userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			 userDealRecord2.setSum(money);
			 userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
			 userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
			 userDealRecord2.setUuid(uuid2);
			 
			if(gainmedal!=null){
				Integer medalNum1 = gainmedal.getMedalNum1();
				Integer medalNum2 = gainmedal.getMedalNum2();
				Integer medalNum3 = gainmedal.getMedalNum3();
				String randomRange = gainmedal.getRandomRange();
				String[] split = randomRange.split(",");
				if(split!=null){
					StringBuilder stringBuilder=new StringBuilder();
					stringBuilder.append(randomRange);
					int a=split.length;
					Random r = new Random(); 
					int indexInt = r.nextInt(a);
					String str = split[indexInt];
					if(str.equals("1")){
						//获得一个偷偷看勋章
						if(medalNum1!=null){
							gainmedal2.setMedalNum1(medalNum1+1);
							if(medalNum1>=0){
								int indexOf = stringBuilder.indexOf("1");
								if(indexOf==0){
									stringBuilder.delete(indexOf, indexOf+2);
								}else if(indexOf>0){
									stringBuilder.delete(indexOf-1, indexOf+1);
								}
								gainmedal2.setRandomRange(stringBuilder.toString());
							}
							
						}else {
							gainmedal2.setMedalNum1(1);
							gainmedal2.setRandomRange("2,3");
							
						}
						if(money>0){
							redEnvelopesMapper.insertSelective(redEnvelopes);
							userDealRecordMapper.insertSelective(userDealRecord1);
							userDealRecordMapper.insertSelective(userDealRecord2);
						}
						userMapper.updateByPrimaryKeySelective(user2);
						gainmedalMapper.updateByPrimaryKeySelective(gainmedal2);
						getMedalNotice(1,user);
						return 1;
					}else if(str.equals("2")){
						if(medalNum2!=null){
							gainmedal2.setMedalNum2(medalNum2+1);
							if(medalNum2>=0){
								int indexOf = stringBuilder.indexOf("2");
								if(indexOf==0){
									stringBuilder.delete(indexOf, indexOf+2);
								}else if(indexOf>0){
									stringBuilder.delete(indexOf-1, indexOf+1);
								}
								gainmedal2.setRandomRange(stringBuilder.toString());
							}
							if(money>0){
								redEnvelopesMapper.insertSelective(redEnvelopes);
								userDealRecordMapper.insertSelective(userDealRecord1);
								userDealRecordMapper.insertSelective(userDealRecord2);
							}
							userMapper.updateByPrimaryKeySelective(user2);
							gainmedalMapper.updateByPrimaryKeySelective(gainmedal2);
							getMedalNotice(2,user);
							return 2;
							
						}else {
							gainmedal2.setMedalNum2(1);
							gainmedal2.setRandomRange("1,3");
						}
					}else if(str.equals("3")){
						if(medalNum3!=null){
							gainmedal2.setMedalNum3(medalNum3+1);
							if(medalNum3>=0){
								int indexOf = stringBuilder.indexOf("3");
								if(indexOf==0){
									stringBuilder.delete(indexOf, indexOf+2);
								}else if(indexOf>0){
									stringBuilder.delete(indexOf-1, indexOf+1);
								}
								gainmedal2.setRandomRange(stringBuilder.toString());
							}
							
						}else {
							gainmedal2.setMedalNum3(1);
							gainmedal2.setRandomRange("1,2");
						}
						if(money>0){
							redEnvelopesMapper.insertSelective(redEnvelopes);
							userDealRecordMapper.insertSelective(userDealRecord1);
							userDealRecordMapper.insertSelective(userDealRecord2);
						}
						userMapper.updateByPrimaryKeySelective(user2);
						gainmedalMapper.updateByPrimaryKeySelective(gainmedal2);
						getMedalNotice(3,user);
						return 3;
						
					}else{
						//随机获得一个
						int b = (int)(1+Math.random()*(3));
						if(b==1){
							// 1偷偷看勋章
							gainmedal2.setMedalNum1(medalNum1+1);
							c=1;
						}else if(b==2){
							gainmedal2.setMedalNum2(medalNum2+1);
							c=2;
						}else{
							gainmedal2.setMedalNum3(medalNum3+1);
							c=3;
						}
						if(money>0){
							redEnvelopesMapper.insertSelective(redEnvelopes);
							userDealRecordMapper.insertSelective(userDealRecord1);
							userDealRecordMapper.insertSelective(userDealRecord2);
						}
						userMapper.updateByPrimaryKeySelective(user2);
						gainmedalMapper.updateByPrimaryKeySelective(gainmedal2);
						getMedalNotice(c,user);
						return c;
					}
				}
					
				 
			}else {

				//第一次
				int a = (int)(1+Math.random()*(3));
				if(a==1){
					// 1偷偷看勋章
					gainmedal2.setMedalNum1(1);
					gainmedal2.setRandomRange("2,3");
					c=1;
				}else if(a==2){
					gainmedal2.setMedalNum2(1);
					gainmedal2.setRandomRange("1,3");
					c=2;
				}else{
					gainmedal2.setMedalNum3(1);
					gainmedal2.setRandomRange("1,2");
					c=3;
				}
				if(money>0){
					redEnvelopesMapper.insertSelective(redEnvelopes);
					userDealRecordMapper.insertSelective(userDealRecord1);
					userDealRecordMapper.insertSelective(userDealRecord2);
				}
				userMapper.updateByPrimaryKeySelective(user2);
				gainmedalMapper.insertSelective(gainmedal2);
				getMedalNotice(c,user);
				return c;
				
			
			}
		} catch (Exception e) {
			e.printStackTrace();
			c=0;
		}
		
		return c;
		
	}
	//打赏勋章
	public int updateAndAddGainmedalTwo(Gainmedal gainmedal,double money,User user){
		int c=0;
		try {
			Gainmedal gainmedal2 = new Gainmedal();
			String phoneNumber = user.getPhoneNumber();
			
			gainmedal2.setPhone(phoneNumber);
			double balance = user.getBalance();
			User user2 = new User();
			user2.setPhoneNumber(phoneNumber);
			user2.setBalance(balance+money);
			Date date = new Date();
			
			//红包
			RedEnvelopes redEnvelopes = new RedEnvelopes();
			String uuid = UUIDGenerator.getUUID();
			redEnvelopes.setMoney(money);
			redEnvelopes.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			redEnvelopes.setReceiveDate(date);
			redEnvelopes.setRedStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			redEnvelopes.setRedType(PayTypeUtil.REDENVELPES_NEWYEAR);
			redEnvelopes.setSendDate(date);
			redEnvelopes.setSendPhoneNum(PayTypeUtil.COMPANY_ACCPUNT);
			redEnvelopes.setUserPhoneNum(phoneNumber);
			redEnvelopes.setUuid(uuid);
			
			
			//流水
			UserDealRecord userDealRecord1 = new UserDealRecord();
			UserDealRecord userDealRecord2 = new UserDealRecord();
			 String uuid1 = UUIDGenerator.getUUID();//用户流水
			 String uuid2 = UUIDGenerator.getUUID();//公司流水
			 userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
			 userDealRecord1.setBusinessNo(uuid);
			 userDealRecord1.setCreateDate(date);
			 userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_ENVELOPES);
			 userDealRecord1.setPaySucTime(date);
			 userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			 userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			 userDealRecord1.setSum(money);
			 userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
			 userDealRecord1.setUserId(phoneNumber);
			 userDealRecord1.setUuid(uuid1);
			 
			 userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
			 userDealRecord2.setBusinessNo(uuid);
			 userDealRecord2.setCreateDate(date);
			 userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_ENVELOPES);
			 userDealRecord2.setPaySucTime(date);
			 userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			 userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			 userDealRecord2.setSum(money);
			 userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
			 userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
			 userDealRecord2.setUuid(uuid2);
			 
			
			if(gainmedal!=null){
				Integer medalNum6 = gainmedal.getMedalNum6();
				if(medalNum6!=null){
					gainmedal2.setMedalNum6(medalNum6+1);
				}else {
					gainmedal2.setMedalNum6(1);
				}
				if(money>0){
					
					redEnvelopesMapper.insertSelective(redEnvelopes);
					userDealRecordMapper.insertSelective(userDealRecord1);
					userDealRecordMapper.insertSelective(userDealRecord2);
				}
				userMapper.updateByPrimaryKeySelective(user2);
				gainmedalMapper.updateByPrimaryKeySelective(gainmedal2);
				getMedalNotice(6,user);
					return 6;
				 
			}else {
				if(money>0){
					
					redEnvelopesMapper.insertSelective(redEnvelopes);
					userDealRecordMapper.insertSelective(userDealRecord1);
					userDealRecordMapper.insertSelective(userDealRecord2);
				}
				userMapper.updateByPrimaryKeySelective(user2);
				gainmedal2.setMedalNum6(1);
				gainmedalMapper.insertSelective(gainmedal2);
				getMedalNotice(6,user);
				return 6;
			
			}
		} catch (Exception e) {
			e.printStackTrace();
			c=0;
		}
		
		return c;
		
	}
	//谁得勋章
	public void getMedalNotice(int a,User user){
		String realName = user.getRealName();
		String str1=null;
		if(realName!=null&&!realName.equals("")){
			 str1="恭喜用户"+realName+"获得一枚";
		}else {
			 str1="恭喜用户"+user.getPhoneNumber()+"获得一枚";
		}
		String str2=null;
		StringBuilder stringBuilder=new StringBuilder();
		String string = redisService.get("medal_code");
		if(a==1){
			str2="偷偷看勋章";
		}else if(a==2){
			str2="咨询勋章";
		}else if(a==3){
			str2="创作勋章";
		}else if(a==4){
			str2="悬赏勋章";
		}else if(a==5){
			str2="分享勋章";
		}else if(a==6){
			str2="打赏勋章";
		}else {
			str2="偷偷看勋章";
		}
		
		if(string==null||string.equals("")){
			
			redisService.set("medal_code", str1+str2, 0);
		}else{
			String[] split = string.split(",");
			if(split.length==5){
				
				int lastIndexOf = string.lastIndexOf(",");
				string = string.substring(0, lastIndexOf);
			}
			stringBuilder.append(str1+str2+","+string);
			
			redisService.set("medal_code", stringBuilder.toString(), 0);
		}
		
	}
}
